package common.kit;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.Set;

import com.jfinal.kit.StrKit;
/**
 * GetMD5Code 用户密码加密
 */
public class MD5 {
	// 全局数组
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    private final static String[] filterWhite = {
    	"94e246c3c8a47de9bfa9b9bafa43316bff28486c",
    	"fddab55a27d54b428e02bf52d2c6c0f61989676b",
    	"4a68a7dbcf59e16adf77e9a1f1a9eab151b1c7e8",
    	"72e35b5bbfacbfddd889e379c8baed865ba38406" ,
    } ;
    
    Set<String> set = new HashSet<String>() ;
    public static String hash = "hash" ;
    
    private static String getSha1String(String inputString) {
		return MD5.sha(inputString + "c19f05a4d982d07b7a6c032baea409841d3c287c") ;
	}
    
    public static void main(String[] args) {
		System.out.println(MD5.getSha1String("http://shop.cavenos.com"));
	}
    
    public MD5() {
    }
    
    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    // 返回形式只为数字
    private static String byteToNum(byte bByte) {
        int iRet = bByte;
        System.out.println("iRet1=" + iRet);
        if (iRet < 0) {
            iRet += 256;
        }
        return String.valueOf(iRet);
    }

    // 转换字节数组为16进制字串
    private static String byteToString(byte[] bByte) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
            sBuffer.append(byteToArrayString(bByte[i]));
        }
        return sBuffer.toString();
    }
    //MD5加密，常用于加密用户名密码，当用户验证时。
    public static String GetMD5Code(String strObj) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            // md.digest() 该函数返回值为存放哈希值结果的byte数组
            resultString = byteToString(md.digest(strObj.getBytes()));
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }

   // SHA加密，与MD5相似的用法，只是两者的算法不同。
    // md5加密  
    public static String md5(String inputText) {  
        return encrypt(inputText, "md5");  
    }  
  
    // sha加密  
    public static String sha(String inputText) {  
        return encrypt(inputText, "sha-1");  
    }  
  
    /** 
     * md5或者sha-1加密 
     * @param inputText  要加密的内容 
     * @param algorithmName 加密算法名称：md5或者sha-1，不区分大小写 
     * @return 
     */  
    private static String encrypt(String inputText, String algorithmName) {  
        if (inputText == null || "".equals(inputText.trim())) {  
            throw new IllegalArgumentException("请输入要加密的内容");  
        }  
        if (algorithmName == null || "".equals(algorithmName.trim())) {  
            algorithmName = "md5";  
        }  
        String encryptText = null;  
        try {  
            MessageDigest m = MessageDigest.getInstance(algorithmName);  
            m.update(inputText.getBytes("UTF8"));  
            byte s[] = m.digest();  
            // m.digest(inputText.getBytes("UTF8"));  
            return hex(s);  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
        return encryptText;  
    }  
  
    // 返回十六进制字符串  
    private static String hex(byte[] arr) {  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < arr.length; ++i) {  
            sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1,  3));  
        }  
        return sb.toString();  
    }  
    
    public static boolean isValid(Long timestamp , String sign) {
		if (null == timestamp || StrKit.isBlank(sign)) {
			return false ;
		}
		return sign.equals(MD5.md5(timestamp + "7ff4f5d3c161e2cfdeb91466792dc489"));
	}
}